-------------------------------------------------------------------------------------
-- -*- coding: utf-8 -*-
--
-- @author:
-- @email:    07291@qq.com
-- @date:     2024/10/17
-- @module:   state_tree
-- @describe:
-- @version:  v1.0
--

-------------------------------------------------------------------------------------

---@type state_base
local state_base = import('bt/bt_framework/state/state_base')
---@type behavior_ret
local behavior_ret = import('bt/bt_plugin/behavior3/behavior_ret')
---@type main_target
local main_target = import('bt/bt_framework/func/main_target')

---@class state_tree
local state_tree = state_base:new()
state_tree.name = nil
state_tree.ctx = nil
state_tree.owner = nil
state_tree.tree = nil
state_tree.isLeaving = false
state_tree.time = 0     -- 运行时间
state_tree.totalTime = 0 -- 总运行时间
state_tree.isInterrupt = true
state_tree.isStop = false
state_tree.needFirstExecute = true
function state_tree:init(name, ctx, owner, btTree, completeTree)
    self.name = name
    self.ctx = ctx
    self.owner = owner
    self.tree = btTree
    self.completeTree = completeTree
    self:clear_data()
end

function state_tree:clear_data()
    self.time = 0
    self.totalTime = 0
    self.isInterrupt = true
    self.isStop = false
    self.needFirstExecute = true
    self:interrupt()
    self:on_clear_data()
end

-- 移除目标时调用，做一些清理工作
function state_tree:on_remove()
    self.time = 0
    self.isInterrupt = true
    self.isStop = false
    self.needFirstExecute = true
    self:interrupt()
end

function state_tree:on_clear_data()

end

function state_tree:on_enter()
    self.isLeaving = false
    self.owner:enter_state(self.name, self.ctx)
end

function state_tree:on_update()
    local ctx = self.ctx
    self.time = self.time + ctx.realDeltaTime
    self.totalTime = self.totalTime + ctx.realDeltaTime
    if not self:is_leaving() then
        self.owner:before_update_state(self.name, ctx)
    end
    if self.tree then
        if not self:is_leaving() then
            self.tree:run()
        end
    end
    if not self:is_leaving() then
        self.owner:last_update_state(self.name, ctx)
    end
end

function state_tree:on_leave()
    self.isLeaving = false
    if self.isInterrupt then
        self:interrupt()
        self.time = 0
        self.isStop = false
    else
        self.isStop = true
    end
    self:set_isInterrupt(true)
    self.owner:leave_state(self.name, self.ctx)
end

-- 中断
function state_tree:interrupt()
    if self.tree then
        self.tree:interrupt()
    end
end

function state_tree:set_leaving(val)
    self.isLeaving = val
end

function state_tree:set_isInterrupt(val)
    self.isInterrupt = val
end

function state_tree:is_leaving()
    return self.isLeaving
end

-- 是否停止运行状态
function state_tree:is_stop()
    return self.isStop
end

-- 是否需要首次执行
function state_tree:need_firstExecute()
    return self.needFirstExecute
end

function state_tree:cancel_firstExecute()
    self.needFirstExecute = false
end

-- 得到运行时间
function state_tree:get_run_time()
    return self.time
end

-- 得到总运行时间
function state_tree:get_total_run_time()
    return self.totalTime
end

function state_tree:on_leaving()
    local isEnd = false
    if self.completeTree then
        local ret = self.completeTree:run()
        if ret == behavior_ret.SUCCESS then
            isEnd = true
        end
    else
        isEnd = true
    end
    if isEnd then
        self.completeTree:interrupt()
        local owner = self.owner
        -- 有打断数据
        local breakMainTargetData = owner:get_breakMainTargetData()
        if breakMainTargetData then
            main_target.set_main_target(owner, breakMainTargetData.mainTarget)
            main_target.remove_main_target(owner, breakMainTargetData.mainTarget)
            main_target.insert_main_target(owner, breakMainTargetData.mainTarget, breakMainTargetData.data, true, true, 1)
        else
            main_target.on_complete_main_target(owner)
            self.needFirstExecute = true
        end
        return owner:get_curMainTarget()
    else
        return false
    end
end


return state_tree
